<template>
  <el-card class="user-card">
    <div slot="header" class="clearfix">
      <el-avatar :size="80" :src="user.avatar">
        <img v-if="gender == 1" src="@/assets/images/man.png" />
        <img v-else-if="gender == 2" src="@/assets/images/woman.png" />
        <img v-else src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png" />
      </el-avatar>
      <div class="name">{{ user.name }}</div>
      <div>{{ user.role }}</div>
    </div>
    <div class="u-item" v-for="(item, index) in items" @click="itemClick(item)" :key="index">
      <img :src="item.icon" />
      {{ item.text }}
    </div>
  </el-card>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'UserCard',
  data: function () {
    return {
      gender: null,
      items: [
        {
          icon: require('@/assets/images/work/icon-msg_s.png'),
          text: '我的消息',
          key: 'MyMessage',
          route: '',
        },
        {
          icon: require('@/assets/images/work/icon-info.png'),
          text: '个人资料',
          key: 'UserDetails',
          route: '',
        },
        {
          icon: require('@/assets/images/work/icon-reset.png'),
          text: '修改密码',
          key: 'ResetPassword',
          route: '',
        },
        {
          icon: require('@/assets/images/work/icon-exit.png'),
          text: '退出登录',
          key: 'LoginOut',
          route: '',
        },
        {
          icon: require('@/assets/images/work/icon-download.png'),
          text: '下载APP',
          key: 'AppDownload',
          route: '',
        },
      ],
      user: {
        name: '哈哈哈',
        role: '',
        avatar: '',
      },
    }
  },
  computed: {
    ...mapGetters(['name', 'avatar', 'roles']),
  },
  watch: {
    '$store.state.user.details': function (nval) {
      if (nval) this.gender = JSON.parse(nval).gender
    },
  },
  created() {
    this.user.name = this.name
    this.user.role = this.roles.join()
    this.gender = this.$store.state.user.details && JSON.parse(this.$store.state.user.details).gender
    try {
      this.user.avatar = this.avatar.includes('http')
        ? this.avatar
        : `${public_config.configuration_api}/api/file-management/files/www/` + this.avatar
    } catch (error) {}
  },
  methods: {
    itemClick(item) {
      this.$emit(`on${item.key}`, true)
    },
  },
}
</script>

<style lang="scss" scoped>
.user-card {
  width: 100%;
  padding: 0.25rem;
  box-sizing: border-box;
  background: #ffffff;
  border-radius: 0.08rem;
  box-shadow: none;
  .u-item {
    padding: 0.145rem 0.4rem;
    cursor: pointer;
    box-sizing: border-box;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-size: 0.16rem;
    color: #222222;
    &:hover {
      color: #2e70ff;
    }
    img {
      width: 0.19rem;
      height: 0.19rem;
      margin-right: 0.3rem;
    }
  }
}

::v-deep .el-card__header {
  text-align: center;
  font-size: 0.14rem;
  color: #666666;
  font-style: normal;
  border-bottom: 1px solid #d8d8d8;
  .name {
    font-size: 0.2rem;
    color: #222222;
    margin: 0.2rem 0 0.1rem 0;
  }
}
</style>
